﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Zebra.DataAccess
{
    public class BaseValidation : IDataErrorInfo
    {
        Dictionary<string, string> validationErrors = new Dictionary<string, string>();

        public void Add(string propertyName, string message)
        {
            if (!validationErrors.ContainsKey(propertyName))
                validationErrors.Add(propertyName, message);
        }

        public void Remove(string propertyName)
        {
            if (validationErrors.ContainsKey(propertyName))
                validationErrors.Remove(propertyName);
        }

        public Boolean IsValid
        {
            get
            {
                return !(validationErrors.Count > 0);
            }
        }

        public string Error
        {
            get
            {
                if (!IsValid)
                {
                    return String.Format("{0} data is invalid.", this.GetType());
                }
                else
                {
                    return null;
                }
            }
        }

        public string this[string propertyName]
        {
            get
            {
                if (validationErrors.ContainsKey(propertyName))
                {
                    return validationErrors[propertyName].ToString();
                }
                else
                {
                    return null;
                }
            }
        }
    }
}
